// .........................................................................
// Title: liquidity_dynamics.do
//
// Implements an analysis of the decay in the liquidity of a corporate bond
// as a function of its age, measured in quarters after issuance
// .........................................................................

* ---------------------------------------
* Secondary market turnover
* ---------------------------------------

* Volume, at cusip-quarter level
use "$raw/trace/trace_enhanced_clean.dta", clear
gen date_q = qofd(trd_exctn_dt)
format %tq date_q
rename cusip_id cusip
gen trade_count = 1
gcollapse (sum) entrd_vol_qt trade_count, by(cusip date_q)
rename entrd_vol_qt total_volume_mm
replace total_volume_mm = total_volume_mm / 1e6
gen year = year(dofq(date_q))
mmerge cusip year using "$tmp/amounts_outstanding_y_mv", unmatched(m)
drop _merge
save "$tmp/trace_enhanced_volumes_q", replace

* ---------------------------------------
* Estimate liquidity curve
* ---------------------------------------

* Add bond age
use "$tmp/trace_enhanced_volumes_q", clear
mmerge cusip using "$raw/cmns/gcap_security_master_cusip.dta", unmatched(m) ukeep(issuance_date)
mmerge cusip using "$tmp/ciq_static_characteristics_processed", unmatched(m) uname(_c_)
replace issuance_date = _c_iq_offer_date if missing(issuance_date)
drop _c_* drop _merge
drop if missing(issuance_date)
gen issuance_q = qofd(issuance_date)
format %tq issuance_q
gsort cusip date_q
by cusip: egen min_date_q = min(date_q)
drop if min_date_q < issuance_q
drop min_date_q 
gen bond_age_q = date_q - issuance_q
save "$tmp/trace_enhanced_volumes_q_age", replace

* Prepare regression data
use "$tmp/trace_enhanced_volumes_q_age", clear
gen _first_quarter_volume = total_volume_mm if bond_age_q == 0
gen _first_quarter_trades = trade_count if bond_age_q == 0
by cusip: egen first_quarter_volume = max(_first_quarter_volume)
by cusip: egen first_quarter_trades = max(_first_quarter_trades)
drop _*
drop if missing(first_quarter_volume) & missing(first_quarter_trades)
drop issuance_date
cap drop year
gen year = year(dofq(date_q))
mmerge cusip year using "$tmp/issue_ratings_merged_sp_moodys_y", unmatched(m)
drop _merge
keep if inlist(rating_cat, "AAA", "AA", "A", "BBB")
save "$tmp/liq_curve_reg_data", replace

* Run the regressions
use "$tmp/liq_curve_reg_data", clear
forval i=0/40 {
    if mod(`i', 5) == 0 {
        di "Processing i=`i'"    
    }
    reg total_volume_mm first_quarter_volume if bond_age_q == `i', robust
    gen _b_volume_`i' = _b[first_quarter_volume]
    gen _se_volume_`i' = _se[first_quarter_volume]    
    reg trade_count first_quarter_trades if bond_age_q == `i', robust
    gen _b_trades_`i' = _b[first_quarter_trades]
    gen _se_trades_`i' = _se[first_quarter_trades]    
}

* Format the output
keep if _n == 1
keep _b* _se*
gen key = 1
reshape long _b_volume_ _se_volume_ _b_trades_ _se_trades_, i(key) j(bond_age_q)
rename _*_ *

* Confidence intervals
gen b_volume_max = b_volume + 1.96 * se_volume
gen b_volume_min = b_volume - 1.96 * se_volume
gen b_trades_max = b_trades + 1.96 * se_trades
gen b_trades_min = b_trades - 1.96 * se_trades
drop key
save "$tmp/liq_curve_reg_results", replace

* Construct the plot (Figure 9)
use "$tmp/liq_curve_reg_results", clear
keep if bond_age_q <= 36

line b_volume b_trades bond_age_q, xlab(0(4)36) lcolor(red blue) lpattern(solid dash) ///
        || rarea b_volume_max b_volume_min bond_age_q, fcolor(gray%20) lcolor(gray%20) ///
        || rarea b_trades_max b_trades_min bond_age_q, fcolor(gray%20) lcolor(gray%20) ///
    legend(label(1 "Quarterly Trading Volume") label(2 "Quarterly Number of Trades") order(1 2)) ///
    graphregion(color(white)) xtitle("Bond Age (Quarters)") ytitle("Decay Curve")

graph export "$graphs/liquidity_decay.pdf", as(pdf) replace
